package com.svs.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.svs.common.constant.UserConstants;
import com.svs.common.utils.StringUtils;
import com.svs.framework.base.service.CrudService;
import com.svs.system.domain.SysDept;
import com.svs.system.domain.SysRole;
import com.svs.system.mapper.master.SysDeptDao;

/**
 * 部门管理 服务实现
 * 
 * @author dujianqiao
 */
@Service
public class SysDeptService extends CrudService<SysDeptDao, SysDept> {
	@Autowired
	private SysDeptDao deptDao ;


	/**
	 * 查询部门管理树
	 * 
	 * @return 所有部门信息
	 */
	public List<Map<String, Object>> selectDeptTree() {
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		List<SysDept> deptList = selectAllList(new SysDept());
		trees = getTrees(deptList, false, null);
		return trees;
	}

	/**
	 * 根据角色ID查询部门（数据权限）
	 *
	 * @param role
	 *            角色对象
	 * @return 部门列表（数据权限）
	 */
	public List<Map<String, Object>> roleDeptTreeData(SysRole role) {
		Long roleId = role.getRoleId();
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		List<SysDept> deptList = selectAllList(new SysDept());
		if (StringUtils.isNotNull(roleId)) {
			List<String> roleDeptList = deptDao.selectRoleDeptTree(roleId);
			trees = getTrees(deptList, true, roleDeptList);
		} else {
			trees = getTrees(deptList, false, null);
		}
		return trees;
	}

	/**
	 * 对象转部门树
	 *
	 * @param menuList
	 *            部门列表
	 * @param isCheck
	 *            是否需要选中
	 * @param roleDeptList
	 *            角色已存在菜单列表
	 * @return
	 */
	public List<Map<String, Object>> getTrees(List<SysDept> deptList, boolean isCheck, List<String> roleDeptList) {

		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		for (SysDept dept : deptList) {
			if (UserConstants.DEPT_NORMAL.equals(dept.getStatus())) {
				Map<String, Object> deptMap = new HashMap<String, Object>();
				deptMap.put("id", dept.getDeptId());
				deptMap.put("pId", dept.getParentId());
				deptMap.put("name", dept.getDeptName());
				deptMap.put("title", dept.getDeptName());
				if (isCheck) {
					deptMap.put("checked", roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
				} else {
					deptMap.put("checked", false);
				}
				trees.add(deptMap);
			}
		}
		return trees;
	}

	/**
	 * 查询部门人数
	 * 
	 * @param parentId
	 *            部门ID
	 * @return 结果
	 */
	public int selectDeptCount(Long parentId) {
		SysDept dept = new SysDept();
		dept.setParentId(parentId);
		return deptDao.selectDeptCount(dept);
	}

	/**
	 * 查询部门是否存在用户
	 * 
	 * @param deptId
	 *            部门ID
	 * @return 结果 true 存在 false 不存在
	 */
	public boolean checkDeptExistUser(Long deptId) {
		int result = deptDao.checkDeptExistUser(deptId);
		return result > 0 ? true : false;
	}


	/**
	 * 新增保存部门信息
	 * 
	 * @param dept
	 *            部门信息
	 * @return 结果
	 */
	public int insertDept(SysDept dept) {
		SysDept info = selectByPrimaryKey(dept.getParentId());
		dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
		return save(dept);
	}

	/**
	 * 修改保存部门信息
	 * 
	 * @param dept
	 *            部门信息
	 * @return 结果
	 */
	public int updateDept(SysDept dept) {
		SysDept info = selectByPrimaryKey(dept.getParentId());
		if (StringUtils.isNotNull(info)) {
			String ancestors = info.getAncestors() + "," + dept.getParentId();
			dept.setAncestors(ancestors);
			updateDeptChildren(dept.getDeptId(), ancestors);
		}
		return update(dept);
	}

	/**
	 * 修改子元素关系
	 * 
	 * @param deptId
	 *            部门ID
	 * @param ancestors
	 *            元素列表
	 */
	public void updateDeptChildren(Long deptId, String ancestors) {
		SysDept dept = new SysDept();
		dept.setParentId(deptId);
		List<SysDept> childrens = selectAllList(dept);
		for (SysDept children : childrens) {
			children.setAncestors(ancestors + "," + dept.getParentId());
		}
		if (childrens.size() > 0) {
			deptDao.updateDeptChildren(childrens);
		}
	}


	/**
	 * 校验部门名称是否唯一
	 * 
	 * @param dept
	 *            部门信息
	 * @return 结果
	 */
	public String checkDeptNameUnique(SysDept dept) {
		Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
		SysDept info = deptDao.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
		if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) {
			return UserConstants.DEPT_NAME_NOT_UNIQUE;
		}
		return UserConstants.DEPT_NAME_UNIQUE;
	}
}
